//
//  YAYIServicePriceCell.m
//  YAYIDoctor
//
//  Created by hxp on 16/5/5.
//  Copyright © 2016年 hxp. All rights reserved.
//

#import "YAYIServicePriceCell.h"

#import "YAYIServiceModel.h"

@interface YAYIServicePriceCell()

@property (nonatomic, weak) UILabel  *itemNameLbl;
@property (nonatomic, weak) UILabel  *priceLbl;
@property (nonatomic, weak) UIButton *selectBtn;
@property (nonatomic, weak) UILabel *seprateLine;

@end


@implementation YAYIServicePriceCell


- (void)setIsSelected:(BOOL)isSelected
{
    _isSelected = isSelected;
    self.selectBtn.selected = isSelected;
}


- (void)setModel:(YAYIServiceModel *)model
{
    _model = model;
    
//    self.itemNameLbl.text = model.title;
//    self.priceLbl.text = [NSString stringWithFormat:@"¥ %.2f", [model.price doubleValue] / 100.0];
    
    CGFloat margin = 10;
    CGSize size = [model.title sizewithFont:self.itemNameLbl.font maxSize:CGSizeMake(self.width, CGFLOAT_MAX)];
    if (size.height > 20) {
        
        margin = 4;
    }
    
    NSString *moneyStr = [NSString stringWithFormat:@"¥ %.2f", [model.price doubleValue] / 100.0];
    NSString *textStr = [NSString stringWithFormat:@"%@\n%@", model.title, moneyStr];
    
    // 调整行间距
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:textStr];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.paragraphSpacing = margin;
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, textStr.length)];
    self.itemNameLbl.attributedText = attributedString;

}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

+ (instancetype)cellWith:(UITableView *)tableview
{
    static NSString *cellID = @"servicePriceCell";
    
    YAYIServicePriceCell *cell = [tableview dequeueReusableCellWithIdentifier:cellID];
    
    if (nil == cell) {
        cell = [[YAYIServicePriceCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    
    return cell;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createViews];
    }
    return self;
}


-(void)createViews
{
    
    UILabel *itemNameLbl = [[UILabel alloc] init];
    itemNameLbl.numberOfLines = 0;
    itemNameLbl.textColor  = YAYIColor(87, 85, 85);
    itemNameLbl.font = [UIFont systemFontOfSize:font15];
    [self.contentView addSubview:itemNameLbl];
    self.itemNameLbl = itemNameLbl;
    
    UILabel *priceLbl = [[UILabel alloc] init];
    priceLbl.textColor  = YAYIColor(87, 85, 85);
    priceLbl.font = [UIFont systemFontOfSize:font15];
    [self.contentView addSubview:priceLbl];
    self.priceLbl = priceLbl;
    
    UIButton *selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    selectBtn.userInteractionEnabled = NO;
    [selectBtn setImage:[UIImage imageNamed:@"blank_icon"] forState:UIControlStateNormal];
    [selectBtn setImage:[UIImage imageNamed:@"checked_icon"] forState:UIControlStateSelected];
    [self.contentView  addSubview:selectBtn];
    self.selectBtn = selectBtn;
    
    UILabel *seprateLine = [UILabel new];
    seprateLine.backgroundColor = YAYIColor(0xdd, 0xdd, 0xdd);
    [self.contentView addSubview:seprateLine];
    self.seprateLine = seprateLine;
}


-(void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat margin = 12;
    CGSize size = [self.itemNameLbl.text sizewithFont:self.itemNameLbl.font maxSize:CGSizeMake(self.width, CGFLOAT_MAX)];
    if (size.height > 20) {
        
        margin = 6;
    }
    
    [self.itemNameLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
//        make.top.mas_equalTo(margin);
        //        make.centerY.mas_equalTo(self.contentView).offset(-10);
        make.right.mas_equalTo(-40);
        make.centerY.mas_equalTo(self.contentView);
    }];
    
    [self.priceLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
//        make.bottom.mas_equalTo(-margin);
        //        make.centerY.mas_equalTo(self.itemNameLbl.mas_bottom).offset(10);
        make.right.mas_equalTo(-40);
    }];
    
    [self.selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView);
        make.right.mas_equalTo(self.contentView).offset(-18);
        make.size.mas_equalTo(CGSizeMake(20,20 ));
    }];
    
    [self.seprateLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(self.contentView.mas_bottom);
        make.height.mas_equalTo(0.5);
    }];

}


-(void)setChecked:(BOOL)checked{
    
    if (checked) {
        [self.selectBtn setBackgroundImage:[UIImage imageNamed:@"check_icon"] forState:UIControlStateNormal];
    }else{
        [self.selectBtn setBackgroundImage:[UIImage imageNamed:@"blank_icon"] forState:UIControlStateNormal];
    }
}
@end
